package com.infogant.dao.core;

import com.infogant.dao.mapper.PersonMapper;
import com.infogant.dto.Person;
import org.skife.jdbi.v2.sqlobject.*;
import org.skife.jdbi.v2.sqlobject.customizers.BatchChunkSize;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;

import java.util.Iterator;
import java.util.List;

/**
 * Created by peter.henell on 19/02/15.
 */
@RegisterMapper(PersonMapper.class)
public abstract class AbstractPersonDAO {

    @SqlQuery("select * from PERSON")
    abstract List<Person> getAll();

    @SqlQuery("select * from PERSON where ID = :id")
    abstract Person findById(@Bind("id") int id);

    @SqlUpdate("delete from PERSON where ID = :id")
    abstract int deleteById(@Bind("id") int id);

    @SqlUpdate("update into PERSON set NAME = :name where ID = :id")
    abstract int update(@BindBean Person person);

    @SqlUpdate("update Person set name = :s.name where id = :s.id")
    abstract void updateName(@BindBean("s") Person person);

    @SqlUpdate("insert into PERSON (ID, NAME) values (:id, :name)")
    protected abstract int insert(@BindBean Person person);

    @SqlBatch("insert into PERSON (id, name) values (:id, :name)")
    @BatchChunkSize(1000)
    protected abstract void insertAll(@BindBean Iterator<Person> somethings);

    @Transaction
    public void twoPersons(Person a, Person b) throws Exception {
        insert(a);
        insert(b);
    }
}
